/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DinnerPanel.java
 *
 * Created on 08.03.2010, 23:05:14
 */

package org.bizon.dinner.common.gui;

import java.awt.event.ActionEvent;


import diiner.common.webaccess.UnAutorizationException_Exception;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import org.bizon.dinner.common.interfaces.NB;

/**
 * 
 * @author Bizon
 */
public class DinnerPanel extends javax.swing.JPanel {

	/** Creates new form DinnerPanel */
	public DinnerPanel() {
            initsec();
		initComponents();
jTable1.getModel().addTableModelListener(new TableModelListener() {
			@Override
			public void tableChanged(TableModelEvent e) {
				calculate();
			}
		});
                calculate();
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed"
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        summTextField = new javax.swing.JTextField();
        reloadButton = new javax.swing.JButton();
        okButton = new javax.swing.JButton();
        cacnelButton = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        newBalanceLabel = new javax.swing.JLabel();
        oldBananceTextField = new javax.swing.JTextField();
        newBalanceTextField = new javax.swing.JTextField();
        oldBalanceLabel = new javax.swing.JLabel();

        jTable1.setModel(getDinnerTableModel());
        jScrollPane1.setViewportView(jTable1);

        jLabel1.setText("Order summ:"); // NOI18N

        summTextField.setHorizontalAlignment(javax.swing.JTextField.TRAILING);
        summTextField.setText("000:00"); // NOI18N
        summTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                summTextFieldActionPerformed(evt);
            }
        });

        reloadButton.setText("reload form"); // NOI18N
        reloadButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                reloadButtonActionPerformed(evt);
            }
        });

        okButton.setText("OK"); // NOI18N
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });

        cacnelButton.setText("Cancel"); // NOI18N
        cacnelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cacnelButtonActionPerformed(evt);
            }
        });

        jButton1.setText("relogin");

        newBalanceLabel.setText("New balance:");

        oldBananceTextField.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        oldBananceTextField.setText("000:00");
        oldBananceTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                oldBananceTextFieldActionPerformed(evt);
            }
        });

        newBalanceTextField.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        newBalanceTextField.setText("000:00");

        oldBalanceLabel.setText("Old balance:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 406, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(newBalanceLabel)
                            .addComponent(jLabel1)
                            .addComponent(oldBalanceLabel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(summTextField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 63, Short.MAX_VALUE)
                            .addComponent(newBalanceTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 63, Short.MAX_VALUE)
                            .addComponent(oldBananceTextField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 63, Short.MAX_VALUE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(reloadButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(okButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cacnelButton)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(oldBananceTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(oldBalanceLabel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(summTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(newBalanceTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(newBalanceLabel)))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 249, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cacnelButton)
                    .addComponent(okButton)
                    .addComponent(reloadButton)
                    .addComponent(jButton1))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

        private void summTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_summTextFieldActionPerformed
            // TODO add your handling code here:
        }//GEN-LAST:event_summTextFieldActionPerformed

        private void oldBananceTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_oldBananceTextFieldActionPerformed
            // TODO add your handling code here:
        }//GEN-LAST:event_oldBananceTextFieldActionPerformed

	private void reloadButtonActionPerformed(ActionEvent evt) {// GEN-FIRST:event_reloadButtonActionPerformed
		try {
			SecuritySystem.getDinnerService().clearData(SecuritySystem.getID());
			getDinnerTableModel().setNewList(SecuritySystem.getDinnerService().getList(SecuritySystem.getID()));
			calculate();
		} catch (UnAutorizationException_Exception ex) {
                    
		}
	}// GEN-LAST:event_reloadButtonActionPerformed

	private void initsec() {
		SecuritySystem.login();
	}

	private void okButtonActionPerformed(ActionEvent evt) {// GEN-FIRST:event_okButtonActionPerformed
		try {
			SecuritySystem.getDinnerService().setList(getDinnerTableModel().getEats(), SecuritySystem.getID());
		} catch (UnAutorizationException_Exception e) {
			e.printStackTrace();
			SecuritySystem.showError(e);
		}
		NB.getINB().exit(0);
	}// GEN-LAST:event_okButtonActionPerformed

        private DinnerTableModel tableModel;
	DinnerTableModel getDinnerTableModel() {
		if (tableModel == null) {
			tableModel = new DinnerTableModel(DinnerController.getInstance().loadEat());
		}
		return tableModel;
	}

	private void cacnelButtonActionPerformed(ActionEvent evt) {// GEN-FIRST:event_cacnelButtonActionPerformed
		NB.getINB().exit(0);
	}// GEN-LAST:event_cacnelButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cacnelButton;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JLabel newBalanceLabel;
    private javax.swing.JTextField newBalanceTextField;
    private javax.swing.JButton okButton;
    private javax.swing.JLabel oldBalanceLabel;
    private javax.swing.JTextField oldBananceTextField;
    private javax.swing.JButton reloadButton;
    private javax.swing.JTextField summTextField;
    // End of variables declaration//GEN-END:variables

    private void calculate() {
          String summ = String.format("%f", tableModel.countSumm());
                summTextField.setText(summ.substring(0, summ.length() - 2));
    }

}
